<template>
  <div class="app-container">
    <!-- 1. 搜索区域优化 -->
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <!-- 常用搜索 -->
      <el-form-item label="文件名称" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入文件名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="文件类型" prop="type">
        <el-select v-model="queryParams.type" placeholder="请选择文件类型" clearable size="small">
          <el-option
            v-for="item in fileTypeOptions"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          />
        </el-select>
      </el-form-item>

      <!-- 更多搜索条件（默认折叠） -->
      <div v-show="showMoreSearch">
        <el-form-item label="类目ID" prop="cid">
          <el-input v-model="queryParams.cid" placeholder="请输入类目ID" clearable size="small" />
        </el-form-item>
        <el-form-item label="用户ID" prop="uid">
          <el-input v-model="queryParams.uid" placeholder="请输入用户ID" clearable size="small" />
        </el-form-item>
      </div>

      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
        <el-button type="text" @click="toggleMoreSearch">{{ showMoreSearch ? '收起' : '更多' }}</el-button>
      </el-form-item>
    </el-form>

    <!-- 操作按钮行 -->
    <el-row :gutter="10" class="mb8">
          <el-col :span="1.5">
            <el-button
              type="primary"
              plain
              icon="el-icon-plus"
              size="mini"
              @click="handleAdd"
              v-hasPermi="['iot:album:add']"
            >新增</el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button
              type="success"
              plain
              icon="el-icon-edit"
              size="mini"
              :disabled="single"
              @click="handleUpdate"
              v-hasPermi="['iot:album:edit']"
            >修改</el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button
              type="danger"
              plain
              icon="el-icon-delete"
              size="mini"
              :disabled="multiple"
              @click="handleDelete"
              v-hasPermi="['iot:album:remove']"
            >删除</el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button
              type="warning"
              plain
              icon="el-icon-download"
              size="mini"
              @click="handleExport"
              v-hasPermi="['iot:album:export']"
            >导出</el-button>
          </el-col>
          <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
        </el-row>

    <!-- 2. 表格优化 -->
    <el-table v-loading="loading" :data="albumList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="主键ID" align="center" prop="id" width="80" />
      <el-table-column label="文件预览" align="center" prop="uri" width="100">
        <template slot-scope="scope">
          <el-image
            v-if="scope.row.type === 10"
            style="width: 60px; height: 60px"
            :src="scope.row.uri"
            :preview-src-list="[scope.row.uri]">
          </el-image>
          <span v-else-if="scope.row.type === 20">视频文件</span>
        </template>
      </el-table-column>
      <el-table-column label="文件名称" align="center" prop="name" :show-overflow-tooltip="true" />
      <el-table-column label="文件类型" align="center" prop="type">
        <template slot-scope="scope">
          <span>{{ formatFileType(scope.row.type) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="文件大小" align="center" prop="size" />
      <el-table-column label="用户ID" align="center" prop="uid" />
      <el-table-column label="创建时间" align="center" prop="createTime" width="160">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="120">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)" v-hasPermi="['iot:album:edit']">修改</el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)" v-hasPermi="['iot:album:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 3. 弹窗表单优化 -->
    <el-dialog :title="title" :visible.sync="open" width="600px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-row>
          <el-col :span="12">
            <el-form-item label="文件名称" prop="name">
              <el-input v-model="form.name" placeholder="请输入文件名称" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="文件类型" prop="type">
              <el-select v-model="form.type" placeholder="请选择文件类型">
                <el-option
                  v-for="item in fileTypeOptions"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value"
                />
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-form-item label="文件路径" prop="uri">
          <el-input v-model="form.uri" placeholder="请输入文件路径" />
          <!-- 如果您有文件上传组件，可以替换这里的输入框 -->
          <!-- <fileUpload v-model="form.uri"/> -->
        </el-form-item>
         <el-row>
          <el-col :span="12">
            <el-form-item label="类目ID" prop="cid">
              <el-input v-model="form.cid" placeholder="请输入类目ID" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
             <el-form-item label="用户ID" prop="uid">
              <el-input v-model="form.uid" placeholder="请输入用户ID" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="文件扩展名" prop="ext">
              <el-input v-model="form.ext" placeholder="例如：jpg, mp4" />
            </el-form-item>
          </el-col>
           <el-col :span="12">
            <el-form-item label="文件大小" prop="size">
              <el-input v-model="form.size" placeholder="单位：字节" />
            </el-form-item>
          </el-col>
        </el-row>
        <!-- aid, isDelete, deleteTime等字段通常由后端自动处理，不在表单中展示 -->
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listAlbum, getAlbum, delAlbum, addAlbum, updateAlbum } from "@/api/iot/album";

export default {
  name: "Album",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      showSearch: true,
      showMoreSearch: false, // 控制更多搜索的显示
      total: 0,
      albumList: [],
      title: "",
      open: false,
      // 文件类型选项
      fileTypeOptions: [
        { value: 10, label: "图片" },
        { value: 20, label: "视频" }
      ],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        cid: null,
        uid: null,
        type: null,
        name: null,
      },
      // 表单参数
      form: {},
      // 表单校验规则（简化）
      rules: {
        name: [
          { required: true, message: "文件名称不能为空", trigger: "blur" }
        ],
        type: [
          { required: true, message: "文件类型不能为空", trigger: "change" }
        ],
        uri: [
          { required: true, message: "文件路径不能为空", trigger: "blur" }
        ]
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询相册管理列表 */
    getList() {
      this.loading = true;
      listAlbum(this.queryParams).then(response => {
        this.albumList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 切换更多搜索
    toggleMoreSearch() {
      this.showMoreSearch = !this.showMoreSearch;
    },
    // 格式化文件类型
    formatFileType(type) {
      const option = this.fileTypeOptions.find(item => item.value === type);
      return option ? option.label : '未知类型';
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        cid: null,
        aid: null,
        uid: null,
        type: 10, // 默认文件类型为图片
        name: null,
        uri: null,
        ext: null,
        size: null,
        isDelete: 0, // 默认0
        createTime: null,
        updateTime: null,
        deleteTime: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加相册管理";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getAlbum(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改相册管理";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateAlbum(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addAlbum(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除相册管理编号为"' + ids + '"的数据项？').then(function() {
        return delAlbum(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('iot/album/export', {
        ...this.queryParams
      }, `album_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
